//
//  HMUserAccount.swift
//  Weibo02
//
//  Created by teacher on 16/4/19.
//  Copyright © 2016年 teacher. All rights reserved.


//  ----->>AuthViewController

import UIKit

class HMUserAccount: NSObject, NSCoding {
    

    
    /**
    {
        "access_token": "ACCESS_TOKEN",
        "expires_in": 1234,
        "remind_in":"798114",
        "uid":"12341234"
    }
    */
    
    var uid: String?
    // 过期时间
    // -[__NSCFNumber length] - unrecognized selector sent to instance 0xb000000000229c43'
    // 代表属性的类型写错啦。把 Int 写成 String 了
//    var expires_in: Int = 0
//    var expires_in: String?  --> 会崩溃
    
    var access_token: String?

    var remind_in: String?
    
    var name: String?
    // 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
//accessToken的生命周期时间 --》过期时间
    var expires_in : NSTimeInterval = 0 {
        didSet{
            // 计算过期时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            
//            print(expiresDate)
        }
        
    }
    
    // 过期时间 ： 当accessToken获取回来的那一刻开始算起， expires_in 秒后的时间
    var expiresDate: NSDate?
//    字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    // MARK: - 实现协议的方法
    // 解档 --> 如何读取
    required init?(coder decoder: NSCoder) {
//        uid = decoder.decodeObjectForKey("uid") as? String
//        //expires_in = decoder.decodeIntegerForKey("expires_in")
//        
//        expires_in = decoder.decodeDoubleForKey("expires_in")
//        access_token = decoder.decodeObjectForKey("access_token") as? String
//        remind_in = decoder.decodeObjectForKey("remind_in") as? String
//        name = decoder.decodeObjectForKey("name") as? String
//        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
//        // 解档过期时间
//        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
        
        uid = decoder.decodeObjectForKey("uid") as? String
        expires_in = decoder.decodeDoubleForKey("expires_in")
        //        expires_in = decoder.decodeIntegerForKey("expires_in")
        access_token = decoder.decodeObjectForKey("access_token") as? String
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
        name = decoder.decodeObjectForKey("name") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        // 解档过期时间
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
    }
    
    // 归档要调用的方法 --> 如何保存
    func encodeWithCoder(encoder: NSCoder) {
//        encoder.encodeObject(uid, forKey: "uid")
//        //        encoder.encodeInteger(expires_in, forKey: "expires_in")
//        encoder.encodeDouble(expires_in, forKey: "expires_in")
//        encoder.encodeObject(access_token, forKey: "access_token")
//        encoder.encodeObject(remind_in, forKey: "remind_in")
//        encoder.encodeObject(name, forKey: "name")
//        encoder.encodeObject(avatar_large, forKey: "avatar_large")
//        // 将过期时间也要归档
//        encoder.encodeObject(expiresDate, forKey: "expiresDate")
        
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeDouble(expires_in, forKey: "expires_in")
        //        encoder.encodeInteger(expires_in, forKey: "expires_in")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(name, forKey: "name")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        // 将过期时间也要归档
        encoder.encodeObject(expiresDate, forKey: "expiresDate")

    }

}
